/*
 * For modifications by Bob Breznak
 * 
 * Copyright (c) 2006, Bob Breznak
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *    * Redistributions of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *    
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Copyright 2006 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.iisageek.client;

import org.gwtwidgets.client.ui.*;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.iisageek.client.dialogs.AboutDialog;

/**
 * The top panel, which contains the 'welcome' message and various links.
 * 
 * @author Bob Breznak
 */
public class TopPanel extends Composite implements ClickListener {

	private HTML signOutLink = new HTML("<a href='javascript:;'>Sign Out</a>");
	private HTML aboutLink = new HTML("<a href='javascript:;'>About</a>");
	private HTML login = new HTML("Logged in as: unknown");
	private static TopPanel singleton;
	
	/**
	 * TopPanel
	 * 
	 * The TopPanel constructor creates a new TopPanel that is ready to be
	 * implemented. The username in the top-right corner is set to "unknown" 
	 */
	public TopPanel() {
		singleton = this;
		
		DockPanel outer = new DockPanel();
		VerticalPanel inner = new VerticalPanel();

		inner.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);

		HorizontalPanel links = new HorizontalPanel();
		links.setSpacing(4);
		links.add(new PNGImage("images/icons/logout.png", 16, 16));
		links.add(signOutLink);
		links.add(new PNGImage("images/icons/about.png", 16, 16));
		links.add(aboutLink);

		outer.add(inner, DockPanel.EAST);
		outer.setCellHorizontalAlignment(inner, HorizontalPanel.ALIGN_RIGHT);
		inner.add(login);
		inner.add(links);

		outer.add(new PNGImage("images/logo.png",189,33),DockPanel.WEST);
		
		signOutLink.addClickListener(this);
		aboutLink.addClickListener(this);

		outer.setWidth("100%");
		initWidget(outer);
		inner.setStyleName("TopPanel");
		links.setStyleName("TopPanelLinks");
	}

	/**
	 * setUsername
	 * 
	 * Sets the username at the top-right of the screen (i.e. "Logged in as: rbreznak")
	 * 
	 * @param username (String)
	 */
	public void setUsername(String username) {
		login.setHTML("Logged in as: <b>"+username+"</b>");
	}
	
	/**
	 * onClick
	 * 
	 * The interface for when the user clicks on the links
	 * 
	 * @param sender (Widget)
	 */
	public void onClick(Widget sender) {
		if (sender == aboutLink) {
			AboutDialog dlg = new AboutDialog("About the LnL Database","The LnL Database is currently in development. Until some issues the site is best viewed under Firefox however it will function the same under IE 5 and Opera. There is an issue with the descriptions frame that causes it to look odd sometimes until you resize the browser.");

			int left = (Window.getClientWidth() - 512) / 2;
			int top = (Window.getClientHeight() - 256) / 2;

			dlg.setPopupPosition(left, top);

			dlg.show();
		}
	}
	
	/**
	 * get
	 * 
	 * Returns the single instance of TopPanel
	 * 
	 * @return The single instance of TopPanel
	 */
	public static TopPanel get() {
		return singleton;
	}
}
